//
//  IMTableView.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "IMTableView.h"
#import "LSMsgCell.h"

@implementation IMTableView
{
    NSMutableArray  *_msgArray;
    BOOL            _beginScroll;
    BOOL            _canScrollToBottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self initTableView];
    _msgArray = [NSMutableArray array];
    _beginScroll        = NO;
    _canScrollToBottom  = YES;
}
-(void)initTableView{
    self.delegate = self;
    self.dataSource = self;
    self.backgroundView  = nil;
    self.backgroundColor = [UIColor clearColor];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.showsVerticalScrollIndicator = NO;
    self.hidden = YES;
}

-(void)bulletNewMsg:(LSMsgModel *)msgModel {
    if (msgModel) {
        if (_msgArray.count > 1000)
        {
            [_msgArray removeObjectsInRange:NSMakeRange(0, 100)];
        }
        
        msgModel.msgAttribText = [LSMsgCell getAttributedStringFromModel:msgModel];
        msgModel.msgHeight = [self calCellHeight:msgModel.msgAttribText];
        [_msgArray addObject:msgModel];
        self.hidden = NO;
        [self reloadData];
        
        if (!_beginScroll)
        {
            if ([self calculateTotalCellHeight] >= self.height) {
                [self scrollToBottom];
                _beginScroll = YES;
            }
        }else{
            [self scrollToBottom];
        }
    }
}

-(void)scrollToBottom{
    if (_canScrollToBottom) {
        [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_msgArray.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
}

-(CGFloat)calculateCellHeight:(NSIndexPath *)indexPath{
    LSMsgModel *msgModel = _msgArray[indexPath.row];
    NSAttributedString *msg = [LSMsgCell getAttributedStringFromModel:msgModel];
    CGRect rect = [msg boundingRectWithSize:CGSizeMake(self.width - 20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    CGFloat cellHeight = rect.size.height + 5;
    return cellHeight;
}

-(CGFloat)calCellHeight:(NSAttributedString *)attribText{
    CGRect rect = [attribText boundingRectWithSize:CGSizeMake(self.width - 20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    CGFloat cellHeight = rect.size.height + 5;
    return cellHeight;
}

-(CGFloat)calculateTotalCellHeight{
    CGFloat totalCellHeight = 0;
    for (LSMsgModel *model in _msgArray) {
//        NSInteger index = [_msgArray indexOfObject:model];
        totalCellHeight += model.msgHeight;
    }
    return totalCellHeight;
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //return [self calculateCellHeight:indexPath];
    
    if (_msgArray.count > indexPath.row)
    {
        LSMsgModel *msgModel = _msgArray[indexPath.row];
        return msgModel.msgHeight+8;
    }
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //to
    
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _msgArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID =@"MsgListCell";
    LSMsgCell *cell = (LSMsgCell *)[self dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[LSMsgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    }
    if (_msgArray.count > indexPath.row) {
        LSMsgModel *msgModel = _msgArray[indexPath.row];
        [cell refreshWithModel:msgModel];
    }
    return cell;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _canScrollToBottom = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat tableViewOffset = self.contentOffset.y + self.frame.size.height;
    if (tableViewOffset + 10 >= [self calculateTotalCellHeight]) {
        _canScrollToBottom = YES;
    }
}

@end
